//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class CurrencyDBManager
    {
        #region Methods
        public static void DeleteCurrency(int CurrencyID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CurrencyDelete");
            db.AddInParameter(dbCommand, "CurrencyID", DbType.Int32, CurrencyID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static Currency GetByCurrencyID(int CurrencyID)
        {
            Currency currency = null;
            if (CurrencyID == 0)
                return currency;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CurrencyLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "CurrencyID", DbType.Int32, CurrencyID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    currency = new Currency();
                    currency.CurrencyID = NopDataHelper.GetInt(dataReader, "CurrencyID");
                    currency.Name = NopDataHelper.GetString(dataReader, "Name");
                    currency.CurrencyCode = NopDataHelper.GetString(dataReader, "CurrencyCode");
                    currency.Rate = NopDataHelper.GetDecimal(dataReader, "Rate");
                    currency.DisplayLocale = NopDataHelper.GetString(dataReader, "DisplayLocale");
                    currency.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    currency.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    currency.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    currency.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                }
            }
            return currency;
        }

        public static CurrencyCollection GetAllCurrencies(bool showHidden)
        {
            CurrencyCollection currencyCollection = new CurrencyCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CurrencyLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Currency currency = new Currency();
                    currency.CurrencyID = NopDataHelper.GetInt(dataReader, "CurrencyID");
                    currency.Name = NopDataHelper.GetString(dataReader, "Name");
                    currency.CurrencyCode = NopDataHelper.GetString(dataReader, "CurrencyCode");
                    currency.Rate = NopDataHelper.GetDecimal(dataReader, "Rate");
                    currency.DisplayLocale = NopDataHelper.GetString(dataReader, "DisplayLocale");
                    currency.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    currency.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    currency.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    currency.UpdatedOn = NopDataHelper.GetDateTime(dataReader, "UpdatedOn");
                    currencyCollection.Add(currency);
                }
            }

            return currencyCollection;
        }

        public static Currency InsertCurrency(string Name, string CurrencyCode, decimal Rate,
           string DisplayLocale, bool Published, int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Currency currency = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CurrencyInsert");
            db.AddOutParameter(dbCommand, "CurrencyID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "CurrencyCode", DbType.String, CurrencyCode);
            db.AddInParameter(dbCommand, "Rate", DbType.Decimal, Rate);
            db.AddInParameter(dbCommand, "DisplayLocale", DbType.String, DisplayLocale);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int CurrencyID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@CurrencyID"));
                currency = GetByCurrencyID(CurrencyID);
            }
            return currency;
        }

        public static Currency UpdateCurrency(int CurrencyID, string Name, string CurrencyCode, decimal Rate,
           string DisplayLocale, bool Published, int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Currency currency = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CurrencyUpdate");
            db.AddInParameter(dbCommand, "CurrencyID", DbType.Int32, CurrencyID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "CurrencyCode", DbType.String, CurrencyCode);
            db.AddInParameter(dbCommand, "Rate", DbType.Decimal, Rate);
            db.AddInParameter(dbCommand, "DisplayLocale", DbType.String, DisplayLocale);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            db.AddInParameter(dbCommand, "UpdatedOn", DbType.DateTime, UpdatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                currency = GetByCurrencyID(CurrencyID);

            return currency;
        }
        #endregion
    }
}
